package cn.sh.org.service.impl;

import cn.sh.basic.exception.CustomException;
import cn.sh.basic.util.BaiduAuditUtils;
import cn.sh.org.domain.Employee;
import cn.sh.org.domain.Shop;
import cn.sh.org.domain.ShopAuditLog;
import cn.sh.org.domain.vo.ShopEchart;
import cn.sh.org.mapper.EmployeeMapper;
import cn.sh.org.mapper.ShopAuditLogMapper;
import cn.sh.org.mapper.ShopMapper;
import cn.sh.org.service.IShopService;
import cn.sh.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sh
 * @since 2022-08-05
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService{

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    @Transactional
    public void settlement(Shop shop) {
//        1. 空参数校验
        if(StringUtils.isEmpty(shop.getName())){
            throw new CustomException("店铺名不可为空");
        }

        //使用百度ai检查店铺名称是否合法
        if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new CustomException("店铺名称不合法");
        }
//        2. 校验店铺是否已经入驻过了
        Shop shop1 =  shopMapper.queryByName(shop.getName());
//        1. 如果入驻过，返回我们自定义异常告诉它已经入驻过了
        if(shop1 != null ){
            throw new CustomException("店铺已经入驻过了!");
        }
//        3. 初始化参数
        //设置店铺为待审核状态
        shop.setState(0);
        //设置注册时间
        shop.setRegisterTime(new Date());

//        4. 先查询该店长账号是否已经存在
        Employee admin = shop.getAdmin();
        Employee emp =  employeeMapper.queryByUserName(admin.getUsername());

        //保存店铺信息
        shopMapper.add(shop);
//        1. 如果存在，检查员工的shop_id是否为null
        if(emp != null && emp.getShop_id() != null){
            throw new CustomException("该员工不合法，请确认后再试!");
        }else if(emp != null){
            emp.setShop_id(shop.getId());
            employeeMapper.update(emp);
        }
        admin.setState(0);
        //保存员工
        employeeMapper.add(admin);
        //更新店铺的店长id
        shop.setAdminId(admin.getId());
        //更新店铺
        shopMapper.update(shop);
        //给员工绑定店铺id
        admin.setShop_id(shop.getId());
        //更新员工
        employeeMapper.update(admin);
    }

    @Override
    public void auditPass(ShopAuditLog log) {
        //审核日志表示审核通过
        log.setState(0);
        //设置审核人
        log.setAuditId(334L);
        //设置审核事件
        log.setAuditTime(new Date());
        //保存审核日志
        shopAuditLogMapper.add(log);
        //查询对应店铺
        Shop shop = shopMapper.selectOne(log.getShopId());
        //修改店铺状态为审核通过
        shop.setState(1);
        //更新店铺信息
        shopMapper.update(shop);

        Employee employee = employeeMapper.selectOne(log.getAuditId());


        SimpleMailMessage sm = new SimpleMailMessage();

        sm.setFrom("1589867195@qq.com");
        sm.setTo(employee.getEmail());
        sm.setSubject("审核信息");
        sm.setText("恭喜你通过审核");

        javaMailSender.send(sm);
    }

    @Override
    public void reject(ShopAuditLog log) {
        //审核日志表示审核通过
        log.setState(-1);
        //设置审核人
        log.setAuditId(334L);
        //设置审核事件
        log.setAuditTime(new Date());
        //保存审核日志
        shopAuditLogMapper.add(log);
        //查询对应店铺
        Shop shop = shopMapper.selectOne(log.getShopId());
        //修改店铺状态为审核通过
        shop.setState(-1);
        //更新店铺信息
        shopMapper.update(shop);

        Employee employee = employeeMapper.selectOne(shop.getAdminId());


        SimpleMailMessage sm = new SimpleMailMessage();

        sm.setFrom("1589867195@qq.com");
        sm.setTo(employee.getEmail());
        sm.setSubject("审核信息");
        sm.setText("<h1>审核失败</h1><br>"+
                "<span>失败原因："+log.getNote()+"</span><br>"+
                "<a href='http://127.0.0.1:8081/#/register2?id="+shop.getId()+"'>点击修改资料</a>");

        javaMailSender.send(sm);
    }

    @Override
    public Map<String, Object> shopEchart() {
        List<ShopEchart> se = shopMapper.shopEchart();
        List<String> x = new ArrayList<>();
        List<Integer> y = new ArrayList<>();
        for (ShopEchart cc : se) {
            if (cc.getState()==null){
                cc.setState("未知状态");
            }else if (cc.getState().equals("1")){
                cc.setState("审核通过");
            }else if (cc.getState().equals("0")){
                cc.setState("待审核");
            }else if(cc.getState().equals("-1")){
                cc.setState("驳回");
            }
            x.add(cc.getState());
            y.add(cc.getCount());
        }
        Map<String,Object> mp = new HashMap<>();
        mp.put("x",x);
        mp.put("y",y);
        return mp;

    }

    @Override
    public void dda(List<Shop> list) {
        shopMapper.dda(list);
    }
}
